Skip to content

Conversation

arifmahmudrana
Copy link

Thanks for your contribution. When I am generating test if the function has error then we add a test for wantErr then adds Subtests. But there should be a return statement also and return if wantErr e.g

t.Run(tt.name, func(t *testing.T) {
	got, err := tt.p.Write(tt.args.record)
	if (err != nil) != tt.wantErr {
		t.Errorf("parquetWriter.Write() error = %v, wantErr %v", err, tt.wantErr)
		return
	}
        //////////////////// this return should be added//////////////////////////
	if tt.wantErr {
		return
	}
        //////////////////// this return should be added//////////////////////////
        
       // There is no point checking Subtests if I want error it may create wrong test result
	if got != tt.want {
		t.Errorf("parquetWriter.Write() = %v, want %v", got, tt.want)
	}
})

@cweill cweill closed this in 307a738 Oct 21, 2025
@cweill
Copy link
Owner

cweill commented Oct 21, 2025

Thank you for identifying this issue!

This has been fixed and merged! 🎉

The problem you identified was real - when a test expects an error (tt.wantErr == true), we shouldn't be checking result values that may be in an undefined state.

The fix:
Tests now include an early return after error validation:

if (err != nil) != tt.wantErr {
    t.Errorf("Foo() error = %v, wantErr %v", err, tt.wantErr)
    continue  // or return for subtests
}
if tt.wantErr {
    return  // <-- NEW: Skip result validation when error is expected
}
if got != tt.want {
    t.Errorf("Foo() = %v, want %v", got, tt.want)
}

This ensures tests don't produce false results by checking return values when an error is expected.

The fix is now in the develop branch and will be included in the next release!

Thanks for the contribution!

cweill added a commit that referenced this pull request Oct 21, 2025
This merge brings in 4 releases worth of improvements:

v1.7.1 - New Features:
- Add -use_go_cmp flag for google/go-cmp support
- Add -version flag to display version information

v1.7.2 - Code Quality:
- Remove unnecessary type conversion in generateTest
- Update VS Code link to gotests-specific configuration

v1.7.3 - Security:
- Update golang.org/x/tools to v0.38.0 to fix CVEs

v1.7.4 - Bug Fixes:
- Add early return when wantErr is true (PR #169)
- Fix -template_params flag not being passed (Issue #149)

See individual release notes for details.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants